package com.shawn.server.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.shawn.commons.utils.CommentType;
import com.shawn.model.vo.*;
import com.shawn.server.interceptor.ThreadContext;
import com.shawn.server.service.MessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/messages")
public class MessageController {

    @Autowired
    private MessageService messageService;

    @GetMapping("/comments")
    public ResponseEntity getCommentsByUserId(@RequestParam(defaultValue = "1") Integer page,
                                              @RequestParam(name = "pagesize", defaultValue = "10") Integer pageSize) {
        Long userId = ThreadContext.getUserId();
        //分页获取所有的点赞评论
        PageResult<MessageVo> pageResult = messageService.getCommentsUsers(userId,page, pageSize, CommentType.COMMENT);
        return ResponseEntity.ok(pageResult);
    }


    @GetMapping("/announcements")
    public ResponseEntity getAllAnnouncement(@RequestParam(defaultValue = "1") Integer page,
                                             @RequestParam(name = "pagesize", defaultValue = "10") Integer pageSize) {
        //分页获取所有的公告
        PageResult<AnnouncementVo> pageResult = messageService.getAllAnnouncement(page, pageSize);
        return ResponseEntity.ok(pageResult);
    }

    @GetMapping("/likes")
    public ResponseEntity getLikeCommentsUser(@RequestParam(defaultValue = "1") Integer page,
                                              @RequestParam(name = "pagesize", defaultValue = "10") Integer pageSize) {
        Long userId = ThreadContext.getUserId();
        //分页获取所有的点赞评论
        PageResult<MessageVo> pageResult = messageService.getCommentsUsers(userId, page, pageSize, CommentType.LIKE);
        return ResponseEntity.ok(pageResult);
    }

    @GetMapping("/loves")
    public ResponseEntity getLoveCommentsUser(@RequestParam(defaultValue = "1") Integer page,
                                              @RequestParam(name = "pagesize", defaultValue = "10") Integer pageSize) {
        Long userId = ThreadContext.getUserId();
        //分页获取所有的喜欢评论
        PageResult<MessageVo> pageResult = messageService.getCommentsUsers(userId, page, pageSize, CommentType.LOVE);
        return ResponseEntity.ok(pageResult);
    }


    @GetMapping("/userinfo")
    public ResponseEntity getUserInfoByHXId(String huanxinId) {
        UserInfoVo userInfoVo = messageService.getUserInfoByHXId(huanxinId);
        log.info("已返回用户信息{}", userInfoVo.toString());
        return ResponseEntity.ok(userInfoVo);
    }

    /**
     * 添加朋友
     *
     * @param map 地图
     * @return {@link ResponseEntity}
     */
    @PostMapping("/contacts")
    public ResponseEntity addFriend(@RequestBody Map<String, Object> map) {
        Integer friendId = (Integer) map.get("userId");
        messageService.addFriend(Long.valueOf(friendId));
        return ResponseEntity.ok("朋友已添加");
    }

    /**
     * 得到朋友列表
     *
     * @param page     页面
     * @param pageSize 页面大小
     * @param nickname 昵称
     * @return {@link ResponseEntity}
     */
    @GetMapping("/contacts")
    public ResponseEntity getFriendList(@RequestParam(defaultValue = "1") Integer page
            , @RequestParam(name = "pagesize", defaultValue = "10") Integer pageSize
            , @RequestParam(name = "keyword", required = false) String nickname) {
        PageResult<ContactVo> pageResult = messageService.getFriendsPage(page, pageSize, nickname);
        return ResponseEntity.ok(pageResult);
    }
}
